package com.css.dzj.dzjjcss.modules.venue.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditService;
import com.css.dzj.dzjjcss.common.util.BeanUtil;
import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.PlatformFileUtil;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.modules.venue.entity.VenueEntity;
import com.css.dzj.dzjjcss.modules.venue.service.VenueService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 场地场所信息Controller
 */
@RestController
@RequestMapping(value = "/venue")
public class VenueController {

    @Autowired
    private VenueService venueService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    PlatformFileUtil platformFileUtil;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize,@RequestParam Integer pageNo){
        String s="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<VenueEntity> publicResults =  venueService.queryPage(pageRequest);
            s = JSON.toJSONString(publicResults);
            sysAuditService.saveSuccessAudit("获取场地场所信息列表成功",Constant.CDCS, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data",publicResults);
        }catch (Exception e){
            sysAuditService.saveFailAudit("获取场地场所信息列表失败",Constant.CDCS,Constant.PT,Constant.PAGE,s,e,request);
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过id查看详情信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id){
        String s="";
        try{
            VenueEntity pEntity = venueService.findById(id);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("获取场地场所信息详情成功",Constant.CDCS, Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data",pEntity);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("获取场地场所信息详情失败",Constant.CDCS,Constant.PT,Constant.SELETE,s,e,request);
            return  R.error(e.getMessage());
        }
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public R save(@RequestBody VenueEntity pEntity){
        String s="";
        try{
            if(StringUtils.isEmpty(pEntity)){
                return R.error("参数不能为空");
            }
            venueService.add(pEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("保存场地场所信息成功",Constant.CDCS, Constant.PT,Constant.ZCADD,s,request);
            return R.ok();

        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("保存场地场所信息失败",Constant.CDCS,Constant.PT,Constant.ZCADD,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 暂存
     */
    @PostMapping("/zcsave")
    public R zcSave(@RequestBody VenueEntity pEntity){
        String s="";
        try {
            if(StringUtils.isEmpty(pEntity)) {
                return R.error("暂存信息不能为空");
            }
            venueService.addOnly(pEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("暂存场地场所信息成功",Constant.CDCS, Constant.PT,Constant.ZCADD,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("保存场地场所信息失败",Constant.CDCS,Constant.PT,Constant.ZCADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody VenueEntity pEntity){
        String s="";
        try {
            VenueEntity oldpEntity = venueService.findById(pEntity.getPlaceId());
            BeanUtil.copyBeanNotEmptyToBean(pEntity,oldpEntity);
            oldpEntity.setUpdateTime(new Date());
            venueService.update(oldpEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("修改场地场所信息成功",Constant.CDCS, Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("修改场地场所信息失败",Constant.CDCS,Constant.PT,Constant.UPDATE,s,e,request);
            return  R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String id){
        String s="";
        try {
            if(venueService.delete(id)){
                s = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("删除场地场所信息成功",Constant.CDCS, Constant.PT,Constant.DEL,s,request);
                return R.ok("操作成功!");
            }else{
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("删除场地场所信息失败",Constant.CDCS,Constant.PT,Constant.DEL,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     *多条件查询场地场所信息
     */
    @GetMapping("/gstbList")
    public R getGstbList(@RequestParam(required = false) String placeName,
                        @RequestParam(required = false) String areaNumber,
                        @RequestParam(required = false) String responsibleDept,
                        @RequestParam(required = false) String chargePerson,
                        @RequestParam(required = false) String placePurpose,
                        @RequestParam(required = false) String status,
                        @RequestParam(required = false) Integer pageSize, @RequestParam Integer pageNo) {
        String s="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<VenueEntity> publicResults = venueService.getGstbList(placeName,areaNumber,responsibleDept,chargePerson,placePurpose,status,pageRequest);
            s = JSON.toJSONString(publicResults);
            sysAuditService.saveSuccessAudit("查询通知公告成功",Constant.GG, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", publicResults);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询通知公告失败",Constant.GG,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 根据查询条件导出场地场所信息
     */
    @GetMapping("/exportByIds")
    public R exportHolidayToExcel(HttpServletResponse response, @RequestParam String ids) {
        String s ="";
        try {
            List<VenueEntity> venueEntity = venueService.queryByIds(ids);
            venueService.exportAdmPenaltyToExcel(response, venueEntity);
            s = JSON.toJSONString(venueEntity);
            sysAuditService.saveSuccessAudit("导出行政处罚成功", Constant.CDCS, Constant.PT, Constant.EXPORT, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("导出行政处罚失败", Constant.CDCS, Constant.YZ, Constant.EXPORT, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 全局导出场地场所信息
     */
    @GetMapping("/export")
    public R export( HttpServletResponse response,
                     @RequestParam(required = false) String placeName,
                     @RequestParam(required = false) String areaNumber,
                     @RequestParam(required = false) String responsibleDept,
                     @RequestParam(required = false) String chargePerson,
                     @RequestParam(required = false) String placePurpose,
                     @RequestParam(required = false) String status) {
        String s = "";
        try{
            List<VenueEntity> venueEntity = venueService.queryByParams(placeName,areaNumber,responsibleDept,chargePerson,placePurpose,status);
            venueService.exportAdmPenaltyToExcel(response, venueEntity);
            s  = JSON.toJSONString(venueEntity);
            sysAuditService.saveSuccessAudit("导出成功", Constant.CDCS, Constant.PT, Constant.EXPORT, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("导出失败", Constant.CDCS, Constant.YZ, Constant.EXPORT, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 交换系统返回检查对象List
     */
    @GetMapping("/queryAllJcssMsg")
    public List queryAllJcss() {
        String s = "";
        try {
            List<VenueEntity> list = venueService.queryAllJcss();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 模板下载
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(){
        String s = "";
        try {
            String fileName = "新增场地场所信息模板.xls";
            platformFileUtil.download(fileName);
            s = JSON.toJSONString(fileName);
            sysAuditService.saveSuccessAudit("场地场所信息模板下载成功", Constant.CDCS, Constant.PT, Constant.DOWNLOAD, s, request);
        }catch (Exception e){
            sysAuditService.saveFailAudit("场地场所信息模板下载失败", Constant.CDCS, Constant.YZ, Constant.DOWNLOAD, s, e, request);
        }
    }

    /**
     *导入
     */
    @PostMapping("/deptExcel")
    public R uploadFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        String s = "";
        try {
            String returnMsg = venueService.uploadFile(file,response);
            s = JSON.toJSONString(returnMsg);
            sysAuditService.saveSuccessAudit("场地场所导入成功", Constant.CDCS, Constant.PT, Constant.IMPORT, s, request);
            return R.ok().put("data", returnMsg);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("场地场所导入失败", Constant.CDCS, Constant.YZ, Constant.IMPORT, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
